package com.wmx.datastructuresandalgorithms;

import java.util.Stack;

public class SingleLinkedListStack {

    private  Node head ;

    class Node{
        private Object data;
        private Node next ;

        Node(Object data){
            this.data =data;
        }
    }


    //添加栈元素

    public void push(Object data){
        Node newNode = new Node(data);
        newNode.next = head ;
        head = newNode;
    }


   //出栈
    public Node pop(){
      if(head == null){
          System.out.printf("栈元素为空");
          return null;
      }
       Node current = head;
       head =head.next;
       return  current;
    }


    //查看栈中的元素
    public int size(){

        Node current = head ;
        if(current == null){
            return 0;
        }

        int size = 0;
        while (true){
            size++;
            if(current.next == null){
                break;
            }

            current = current.next;
        }

        return size;
    }


    public static void main(String[] args) {
     /*   SingleLinkedListStack singleLinkedListStack = new SingleLinkedListStack();
        singleLinkedListStack.push(5);
        singleLinkedListStack.push(3);
        singleLinkedListStack.push(4);
        singleLinkedListStack.push(2);
        System.out.println(singleLinkedListStack.pop().data);
        System.out.println(singleLinkedListStack.pop().data);
        System.out.println(singleLinkedListStack.pop().data);
        System.out.println(singleLinkedListStack.pop().data);
        System.out.println(singleLinkedListStack.size());*/
        Stack<Integer> stack = new Stack<>();
        stack.push(2);
        stack.push(4);
        stack.push(6);
        System.out.println(stack.peek());
    }
}
